<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace App\Views\Lib;

use Sun\Sun;
use Sun\Html;
use Sun\Html\Form;
use App\Models;

class Inventory extends Main {
    private $edit;
    public function __construct() {
        parent::__construct();
        $this->edit = new Models\Edition();
        $this->data_scene = 'Body';
    }

    public function SearchResultScene() {
        $edits = $this->edit->SimlpeSearch($this->data['EditType'], $this->data['SearchVal']);
        $rows = array();
        foreach ($edits as $e) {
            $rows[] = array($this->edit->GetEditionTextBrief($e[0]));
        }
        $table = new Html\QueryTable($rows, array("Описание"));
        $table->First()->id = "SearchResult";
        $rows = $table->First()->Childs();
        $rows[0]->AddData();
        for ($i = 1; $i < count($rows); ++$i){
            $rows[$i]->SetAttribute("data-edit", $edits[$i - 1][0])->AddData()
                    ->Added()->SetAttribute("style", "width: 4px;")
                    ->Add(new Html\A(array('href' => "?")))
                    ->Added()->Add(new Html\Img(array(
                        'src' => "pub/media/images/add_16.png",
                        'alt' => "Добавить",
                        'class' => "NoneBorder")));
        }
        echo $table;
    }

    public function AddStatusScene() {

    }

    public function EditTypes() {
        $types = $this->edit->GetEditionTypes();
        $select = new Form\Select(array('id' => "EditType"));
        $select->Add(new Form\Option("-1", "Все"));
        foreach ($types as $type) {
            $select->AddOption($type['type_id'], $type['name']);
        }
        return $select;
    }

    public function ItemStats() {
        $types = $this->edit->GetItemStats();
        $select = new Form\Select(array('id' => "ItemStat"));
        foreach ($types as $type) {
            $select->Add(new Form\Option($type['stat_id'], $type['name']));
        }
        return $select;
    }

    public function Fields() {
        $select = new Form\Select(array('id' => "SelectField", 'style' => "max-width: 200px"));
        $fields = $this->edit->GetFieldsData();
        $select->Add(new Form\Option("-1", "Все поля"));
        foreach ($fields as $field) {
            if ($field['sense'] == "STR") {
                $option = new Form\Option($field['field_id'], $field['name']);
                $select->Add($option);
            }
        }
        return $select;
    }

    public function ItemBrief() {
        return $this->edit->GetEditionTextBrief($this->data['edit_id']);
    }

}
